package br.sc.senai.model.dao;

import br.sc.senai.model.exception.DAOExcption;
import br.sc.senai.model.util.Sup;
import br.sc.senai.model.util.HibernateUtil;
import br.sc.senai.model.util.LogUtil;
import java.io.Serializable;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.exception.ConstraintViolationException;
import org.hibernate.exception.SQLGrammarException;

/**
 *
 * @author jefferson_augusto
 */
public class DataBaseSession {
    
    private static  SessionFactory sessionFactory;
    private static Session session;
    
    public static Session openSession() {
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            if (session == null || !session.isOpen())
                session = sessionFactory.openSession();
            return session;
        } catch (Exception e) {
            LogUtil.addError("DataBaseSession", "Falha ao abrir sessão.", true, e);
        }
        return null;
    }
    
    public static void closeSession() {
        try {
            if (session != null && session.isOpen())
                session.close();
        } catch (Exception e) {
            LogUtil.addError("DataBaseSession", "Falha ao abrir sessão.", true, e);
        }
    }
    
}
